import { createRouter, createWebHashHistory } from 'vue-router'
import Layout from '@/layout/index.vue'

// 匹配views里面所有的.vue文件，动态引入
const modules = import.meta.glob('/src/views/**/*.vue')
//
export function getModulesKey() {
    return Object.keys(modules).map((item) => item.replace('/src/views/', '').replace('.vue', ''))
}


// 使用完整路径确保组件正确加载
const routes = [
  {
    path: '/',
    redirect: '/user'
  },
  {
    path: '/',
    component: Layout,
    children: [
      {
        path: 'user',
        name: 'user', 
        component: () => import('@/views/user/index.vue'),
        meta: { requiresAuth: true, title: '用户管理' }
      },{
        path: 'distributor',
        name: 'distributor', 
        component: () => import('@/views/distributor/index.vue'),
        meta: { requiresAuth: true, title: '代理管理' }
      },{
        path: 'agent',
        name: 'agent', 
        component: () => import('@/views/agent/index.vue'),
        meta: { requiresAuth: true, title: 'Agent管理' }
      },{
        path: 'vip',
        name: 'vip', 
        component: () => import('@/views/vip/index.vue'),
        meta: { requiresAuth: true, title: '会员费管理' }
      },{
        path: 'gold',
        name: 'gold', 
        component: () => import('@/views/gold/index.vue'),
        meta: { requiresAuth: true, title: '虚拟币管理' }
      },{
        path: 'banner',
        name: 'banner', 
        component: () => import('@/views/banner/index.vue'),
        meta: { requiresAuth: true, title: '素材管理' }
      },{
        path: 'finance',
        name: 'finance', 
        redirect: 'finance/pay',
        meta: { requiresAuth: true, title: '财务管理' },
        children: [
          {
            path: 'pay',
            name: 'pay', 
            component: () => import('@/views/finance/pay.vue'),
            meta: { requiresAuth: true, title: '支付记录' }
          },
          {
            path: 'consumption',
            name: 'consumption', 
            component: () => import('@/views/finance/consumption.vue'),
            meta: { requiresAuth: true, title: '消费记录' }
          },
        ]
      },{
        path: 'system',
        name: 'system', 
        redirect: 'system/menu',
        meta: { requiresAuth: true, title: '系统管理' },
        children: [
          {
            path: 'menu',
            name: 'menu', 
            component: () => import('@/views/system/menu/index.vue'),
            meta: { requiresAuth: true, title: '菜单管理' }
          },
          {
            path: 'role',
            name: 'role', 
            component: () => import('@/views/system/role/index.vue'),
            meta: { requiresAuth: true, title: '角色管理' }
          },
          {
            path: 'admin',
            name: 'admin', 
            component: () => import('@/views/system/admin/index.vue'),
            meta: { requiresAuth: true, title: '用户管理' }
          },
        ]
      }

      // 其他需要 Layout 的路由...
    ]
  },
  // 不需要 Layout 的路由（如登录页）
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/login.vue')
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes,
})

export default router
